package com.yzr.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author : yzr
 * @description 自定义线程池
 * @date : 2021/7/22
 */
@Configuration
public class ThreadPoolConfiguration {

    @Value("${thread-pool.corePoolSize:2}")
    private Integer corePoolSize;

    @Value("${thread-pool.maxPoolSize:3}")
    private Integer maximumPoolSize;

    @Value("${thread-pool.queueSize:10}")
    private Integer queueSize;

    @Value("${thread-pool.keepAliveTime:600}")
    private Long keepAliveTime;

    @Bean("workerPool")
    public ThreadPoolExecutor threadPool() {
        // 初始化线程池
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(queueSize), new CustomizableThreadFactory("spring-workerPool"));
    }
}